package com.woniuxy.Request;


import com.woniuxy.Bean.User;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Enumeration;
import java.util.Map;

/**
 * @auther linke
 * @create 2021-10-16 9:38
 * 测试获取请求参数
 */
@WebServlet("/requestDemo01")
public class RequestDemo01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");//设置字符串解析编码
        /**
         * 获取请求行
         */
        String method = req.getMethod();
        System.out.println(method);//POST

        String contextPath = req.getContextPath();
        System.out.println(contextPath);//

        String servletPath = req.getServletPath();
        System.out.println(servletPath);//  /requestDemo01

        String requestURI = req.getRequestURI();
        System.out.println(requestURI);//   /requestDemo01

        StringBuffer requestURL = req.getRequestURL();
        System.out.println(requestURL);//   http://localhost:8080/requestDemo01

        String queryString = req.getQueryString();
        System.out.println(queryString);//  null    提交方式为post

        String protocol = req.getProtocol();
        System.out.println(protocol);//HTTP/1.1

        String remoteAddr = req.getRemoteAddr();
        System.out.println(remoteAddr);//0:0:0:0:0:0:0:1

        /**
         * 获取请求头
         */
        String accp = req.getHeader("accp");
        System.out.println(accp);//null

        Enumeration<String> headerNames = req.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String headName = headerNames.nextElement();
            System.out.print(headName + "...");
            System.out.print(req.getHeader(headName));
            System.out.println();
        }


        Enumeration<java.lang.String> hubby = req.getHeaders("hubby");
        while (hubby.hasMoreElements()) {
            String hub = hubby.nextElement();
            System.out.println(hub);//-1
        }


        int intHeader = req.getIntHeader("username");
        System.out.println(intHeader);

        /**
         * 获取请求体
         * 只有post请求有请求体
         */
       /* ServletInputStream inputStream = req.getInputStream();
        byte[] bys = new byte[1024];
        int len = inputStream.read(bys);
        String str = new String(bys, 0, len);
        System.out.println(str);//username=aaa&password=sss&hubby=cat&hubby=dog&hubby=pig*/

     /*   String read = req.getReader().readLine();
        System.out.println(read);*/

        /**
         * 获取请求参数通用方法
         */
        String usernamevalue = req.getParameter("username");
        System.out.println(usernamevalue);//null

        Enumeration<String> parameterNames = req.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String parameterName = parameterNames.nextElement();
            String parameterValue = req.getParameter(parameterName);
            System.out.println(parameterName + "..." + parameterValue);
        }
/*
        String[] hubbies = req.getParameterValues("hubby");
        for (String s : hubbies) {
            System.out.println();
            System.out.print(s + "...");
        }*/

        User user = new User();
        System.out.println(user);//添加数据前    User{id=0, username='null', password='null', hubby=null}
        Map<String, String[]> parameterMap = req.getParameterMap();
        try {
            BeanUtils.populate(user, parameterMap);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        System.out.println(user);//添加数据后    User{id=0, username='null', password='null', hubby=null}

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
